package com.relife.common.utils;

import java.util.List;

import com.relife.common.model.ClientManager;
import com.relife.core.entity.AppEntity;
import com.relife.core.entity.SmsEntity;
import com.relife.core.service.BaseService;
import com.relife.core.spring.SpringContextUtil;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;


public class SmsUtil {
	private static final Logger log = Logger.getLogger("SmsUtil");
	private static BaseService service = (BaseService) SpringContextUtil.getService("service");
	private static boolean debug = Boolean.valueOf(SysConfig.getProperty("debug"));

	/**
	 * 发送短信
	 * 
	 * @author WU
	 * @date 2016年11月30日 上午11:11:46
	 * @param phone
	 * @param content
	 */
	public static void sendSMS(final String phone, final String content) {
		if (!debug) {
			Thread tr = new Thread(new Thread() {
				public void run() {
					if (StringUtil.isNotEmpty(phone) && StringUtil.isNotEmpty(content)) {
						String[] phones = phone.split(",");
						for (int i = 0; i < phones.length; i++) {
							GuanfangSmsUtil.sendMsg(phones[i], content);
							SmsEntity sms = new SmsEntity();
							sms.setContent(content);
							sms.setPhone(phones[i]);
							service.save(sms);
						}
					} else {
						log.warn("发送号码：" + phone + ",或内容：" + content + "不能为空");
					}
				}
			});
			tr.start();
		}
	}

	/**
	 * 发送短信
	 * 
	 * @author WU
	 * @date 2017年3月20日 下午4:09:41
	 * @param mobiles
	 * @param params
	 * @param templateid
	 * @param content
	 */
	public static void sendSMS(final JSONArray mobiles, final JSONArray params, final String templateid, final String content) {
		if (!debug) {
			Thread tr = new Thread(new Thread() {
				public void run() {
					for (int i = 0; i < mobiles.size(); i++) {
						SmsEntity sms = new SmsEntity();
						sms.setContent(content);
						sms.setPhone(mobiles.get(i).toString());
						service.save(sms);
					}
					AppEntity app = ClientManager.getInstance().getApp();

					if (mobiles.size() < 100) {
						NeteaseSmsUtil.sendTemplate(app.getSmsAppKey(), app.getSmsAppSecret(), mobiles, params, templateid);
					} else {
						int k = (mobiles.size() - mobiles.size() % 100) / 100;
						for (int i = 0; i < k; i++) {
							JSONArray phones = SmsUtil.List2JSONArray(mobiles.subList(100 * i, (i + 1) * 100));
							NeteaseSmsUtil.sendTemplate(app.getSmsAppKey(), app.getSmsAppSecret(), phones, params, templateid);
						}
						JSONArray phones = SmsUtil.List2JSONArray(mobiles.subList(100 * k, mobiles.size()));
						NeteaseSmsUtil.sendTemplate(app.getSmsAppKey(), app.getSmsAppSecret(), phones, params, templateid);
					}

				}
			});
			tr.start();
		}
	}

	/**
	 * 
	 * @author WU
	 * @date 2017年5月5日 下午1:52:18
	 * @param list
	 * @return
	 */
	public static JSONArray List2JSONArray(List<Object> list) {
		JSONArray array = new JSONArray();
		for (int i = 0; i < list.size(); i++) {
			array.add(list.get(i));
		}
		return array;
	}
}
